package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.bean.CashierPayChannel;
import com.xb.loan.cashier.biz.dao.bean.CashierPayChannelType;
import com.xb.loan.cashier.biz.dao.bean.CashierPlat;
import com.xb.loan.cashier.biz.service.CashierPayChannelService;
import com.xb.loan.cashier.biz.service.CashierPayChannelTypeService;
import com.xb.loan.cashier.biz.service.CashierPlatPaytypeService;
import com.xb.loan.cashier.biz.service.CashierPlatService;
import com.xb.loan.cashier.service.ICashierConfigManagerService;
import com.xb.loan.cashier.service.bean.CashierPayChannelBO;
import com.xb.loan.cashier.service.bean.CashierPayChannelTypeBO;
import com.xb.loan.cashier.service.bean.CashierPlatBO;
import com.xb.loan.cashier.service.exception.ICashierServiceException;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.common.ObjectOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("cashierConfigManagerService")
public class CashierConfigManagerServiceImpl implements
        ICashierConfigManagerService {

	private static final Logger log = LoggerFactory.getLogger(CashierConfigManagerServiceImpl.class);
	
	@Autowired
	private CashierPlatService cashierPlatService;
	
	@Autowired
	private CashierPayChannelService cashierPayChannelService;
	
	@Autowired
	private CashierPlatPaytypeService cashierPlatPaytypeService;
	
	@Autowired
	private CashierPayChannelTypeService cashierPayChannelTypeService;
	
	@Override
	public List<Map<String, Object>> queryPayChannelTypeByPage(int pageNum,
                                                               int pageSize) {
		return cashierPayChannelTypeService.queryPayChannelTypeByPage(pageNum, pageSize);
	}

	@Override
	public int queryPayChannelTypeCount() {
		return cashierPayChannelTypeService.queryPayChannelTypeCount();
	}

	@Override
	public List<CashierPayChannelTypeBO> queryPayChannelTypeListAll() {
		List<CashierPayChannelType> list = cashierPayChannelTypeService.queryPayChannelTypeListAll();
		return ObjectOperation.convertByClassesList(list, CashierPayChannelTypeBO.class);
	}

	@Override
	public void savePayChannel(CashierPayChannelBO payChannel)
			throws BizException {
		this.clearCacheAll();
		CashierPayChannel cpc = ObjectOperation.convertByClasses(payChannel, CashierPayChannel.class);
		cashierPayChannelService.savePayChannel(cpc);
	}

	@Override
	public void delPayChannel(String payChannelId) throws BizException {
		this.clearCacheAll();
		cashierPayChannelService.delPayChannel(payChannelId);
	}

	@Override
	public void updatePayChannel(CashierPayChannelBO payChannel)
			throws BizException {
		this.clearCacheAll();
		CashierPayChannel cpc = ObjectOperation.convertByClasses(payChannel, CashierPayChannel.class);
		cashierPayChannelService.updatePayChannel(cpc);
	}

	@Override
	public List<Map<String, Object>> queryPayChannelList() {
		return cashierPayChannelService.queryPayChannelList();
	}

	@Override
	public CashierPayChannelBO selectByChannelId(String payChannelId) {
		CashierPayChannel cpc = cashierPayChannelService.selectByPayChannelId(payChannelId);
		return ObjectOperation.convertByClasses(cpc, CashierPayChannelBO.class);
	}

	@Override
	public void saveCashierPlat(CashierPlatBO cashierPlat, String[] payChannels)
			throws BizException {
		this.clearCacheAll();
		CashierPlat cp = ObjectOperation.convertByClasses(cashierPlat, CashierPlat.class);
		cashierPlatService.saveOrUpdateCashierPlat("save", cp, payChannels);
	}

	@Override
	public void updateCashierPlat(CashierPlatBO cashierPlat,
			String[] payChannels) throws BizException {
		this.clearCacheAll();
		CashierPlat cp = ObjectOperation.convertByClasses(cashierPlat, CashierPlat.class);
		cashierPlatService.saveOrUpdateCashierPlat("update", cp, payChannels);
	}

	@Override
	public void deleteCashierPlat(String platId) throws BizException {
		this.clearCacheAll();
		cashierPlatService.deleteCashierPlat(platId);
	}

	@Override
	public List<CashierPlatBO> queryCashierPlatListAll() {
		List<CashierPlat> list = cashierPlatService.queryCashierPlatListAll();
		return ObjectOperation.convertByClassesList(list, CashierPlatBO.class);
	}

	@Override
	public void updateCashierPlatStatus(String platId, String enbale)
			throws BizException {
		this.clearCacheAll();
		cashierPlatService.updateCashierPlatStatus(platId, enbale);
	}

	@Override
	public List<CashierPayChannelBO> queryPayChannelsByPlatId(String platId) {
		List<CashierPayChannel> list = cashierPayChannelService.queryPayGateWayById(platId);
		return ObjectOperation.convertByClassesList(list, CashierPayChannelBO.class);
	}
	/**
	 * 清除收银台所有相关缓存
	 * @throws BizException
	 * @author zljun
	 * @date 2016年6月16日
	 */
	private void clearCacheAll() throws BizException {
		try {
			((CashierPlatServiceImpl)cashierPlatService).clearCacheAll();
			((CashierPayChannelServiceImpl)cashierPayChannelService).clearCacheAll();
			((CashierPlatPaytypeServiceImpl)cashierPlatPaytypeService).clearCacheAll();
			((CashierPayChannelTypeServiceImpl)cashierPayChannelTypeService).clearCacheAll();
		} catch (Exception e) {
			log.error("清除收银台缓存异常",e);
			throw ICashierServiceException.systemException;
		}
		
	}

}
